<template>
	<u-avatar mode="aspectFill" :text="text" fontSize="18" :src="avatar" :size="size" randomBgColor />
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	export default {
		props: {
			user: {
				type: Object,
				default: () => {}
			},
			size: {
				type: String,
				default: '50rpx'
			}
		},
		computed: {
			avatar() {
				if (this.user && this.user.avatar) {
					return baseUrl + this.user.avatar;
				} else {
					return ''
				}
			},
			text() {
				const size = this.size.replace(/\D/g, "");
				let count = 2;
				if (size < 64) {
					count = 1;
				}
				if (!this.user) {
					return '';
				}
				if (this.user.avatar) {
					return ''
				}
				const {
					nickName,
					author,
					createBy
				} = this.user;
				return (nickName || author || createBy || '').slice(0, count);
			}
		}
	}
</script>